package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemMultiGridImageBinding;
import com.jwdf.uda.databinding.ItemTextRecommendBinding;
import com.jwdf.uda.databinding.ViewDesignerItemBottomBinding;
import com.jwdf.uda.databinding.ViewRecommendItemTopBinding;
import com.jwdf.uda.event.GuideLikeListEvent;
import com.jwdf.uda.event.GuideTabCountEvent;
import com.jwdf.uda.event.VoteDelEvent;
import com.jwdf.uda.event.VoteTabCountEvent;
import com.jwdf.uda.ui.activity.GuideDetailActivity;
import com.jwdf.uda.ui.activity.ReplyAnswerCmtActivity;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.item.HomeRecommendItem;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ShareUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.GridImageThreeLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/11.
 */
public class RecommendAdapter extends AbsAdapter<HomeRecommendItem> {
    private static final int ITEM_SINGLE_IMAGE = 1;
    private static final int ITEM_MULTI_IMAGE = 2;
    private  boolean mIsUser=true;
    private boolean mIsLike = false;//是否点赞

    public RecommendAdapter(Context context,boolean isUser) {
        super(context);
        mIsUser=isUser;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case ITEM_SINGLE_IMAGE:
                return new SingleImageHolder(mInflater.inflate(R.layout.item_multi_grid_image, parent, false));
            case ITEM_MULTI_IMAGE:
                return new MultiImageHolder(mInflater.inflate(R.layout.item_multi_grid_image, parent, false));
            default:
                return new TextHolder(mInflater.inflate(R.layout.item_text_recommend, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        PbBean.PbGuideSimple bean = getItem(position).recommend;
        if (bean.getImageListList() == null || bean.getImageListCount() == 0) {
            return 0;
        } else if (bean.getImageListCount() == 1) {
            return ITEM_SINGLE_IMAGE;
        } else {
            return ITEM_MULTI_IMAGE;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    public boolean getIsUser(){
        return mIsUser;
    }

    private void onBindCommonView(View convertView, ViewRecommendItemTopBinding topLayout, ViewDesignerItemBottomBinding bottomLayout, View ivRecommendNew, HomeRecommendItem item) {
        final PbBean.PbGuideSimple recommend = item.recommend;

        if(recommend.getIsNew()>0){
            topLayout.ivDesignerNew.setVisibility(View.VISIBLE);
        }else {
            topLayout.ivDesignerNew.setVisibility(View.GONE);
        }
        topLayout.tvName.setText(recommend.getUserInfo().getNickname());
        topLayout.tvIntro.setText(recommend.getUserInfo().getIntroduction());
        topLayout.tvIntro.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(recommend.getUserInfo().getIntroduction()))?View.GONE:View.VISIBLE);
        topLayout.tvContent.setText(recommend.getContent());
        topLayout.ivIsDesigner.setVisibility(recommend.getUserInfo().getIsDesigner()?View.VISIBLE:View.GONE);
        ImageLoader.getInstance().displayImage(recommend.getUserInfo().getAvatar().getUrl(), topLayout.ivHeader);
        bottomLayout.tvZanCount.setText(String.valueOf(item.likeCount));
        bottomLayout.tvZanCount.setSelected(recommend.getIsLike());
        bottomLayout.tvCommentCount.setText(String.valueOf(item.commentCount));
        topLayout.setIsDesigner(recommend.getUserInfo().getIsDesigner());
        mIsLike=recommend.getIsLike();
        if (!getIsUser()){
            bottomLayout.llDesignerBottom.setVisibility(View.GONE);
        }
                switch (recommend.getStatus()) {
                    case GUIDE_STATUS_NONE:
                        topLayout.tvStatus.setText("无状态");
                        break;
                    case GUIDE_STATUS_WAIT:
                        topLayout.tvStatus.setText("待响应");
                        break;
                    case GUIDE_STATUS_UNDERWAY:
                        topLayout.tvStatus.setText("服务中");
                        break;
                    case GUIDE_STATUS_DESIGNER_FINISHED:
                        topLayout.tvStatus.setText("完成待确认");
                        break;
                    case GUIDE_STATUS_BACK:
                        topLayout.tvStatus.setText("进行中");
                        break;
                    case GUIDE_STATUS_EVALUATION:
                        topLayout.tvStatus.setText("强制结束");
                        break;
                    case GUIDE_STATUS_FINISHED:
                        topLayout.tvStatus.setText("完成已确认");
                        bottomLayout.llDesignerBottom.setVisibility(View.VISIBLE);
                        break;
                    case GUIDE_STATUS_CANCEL:
                        topLayout.tvStatus.setText("已取消");
                        break;
                }

        if(item != null && item.recommend != null){
            ivRecommendNew.setVisibility(item.recommend.getIsNew()>0 ? View.VISIBLE:View.GONE);
        }else {
            ivRecommendNew.setVisibility(View.GONE);
        }

        bottomLayout.llZanCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doPraiseGuideAction(recommend.getId(),recommend.getIsLike());
            }
        });
        bottomLayout.llVoteCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recommend != null && !TextUtils.isEmpty(recommend.getShareUrl())) {
                    ShareUtil.onekeyShare(v.getContext(), recommend.getContent(), recommend.getShareUrl(),"");
                }
            }
        });
        bottomLayout.llCommentCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReplyAnswerCmtActivity.startActivity(v.getContext(),recommend.getId(),PbBean.PbSignNavType.LINK_TYPE_GUIDE);
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener(v.getContext(), recommend, 0);

            }
        });
    }

    /**
     * 点赞请求
     */
    public void doPraiseGuideAction(int guideId,boolean isLike) {
        if (isLike) {
            EventBusUtil.post(new GuideLikeListEvent(GuideLikeListEvent.EVENT_GUIDE_UNLIKE,guideId));
        } else {
            EventBusUtil.post(new GuideLikeListEvent(GuideLikeListEvent.EVENT_GUIDE_LIKE,guideId));
        }
    }

    //点击选项处理
    private void onItemClickListener(Context context, PbBean.PbGuideSimple item, int tabIndex) {
        GuideDetailActivity.startActivityForTab(context, item.getId(), tabIndex);
    }

    public void onEventMainThread(VoteDelEvent event) {
        if(mData != null || event != null) {
            for(HomeRecommendItem item : mData) {
                PbBean.PbGuideSimple recommend = item.recommend;
                if (recommend.getId() == event.getVoteId()) {
                    mData.remove(recommend);
                    notifyDataSetChanged();
                }
            }
        }
    }
    public void onEventMainThread(VoteTabCountEvent event) {
        if(mData != null || event != null) {
            for(HomeRecommendItem item : mData) {
                PbBean.PbGuideSimple recommend = item.recommend;
                if (recommend.getId() == event.getVoteId()) {
                    if(event.isCommentType()){
                        item.commentCount = event.getCommentCount();
                    }else if(event.isVoteType()){
                        item.voteCount = event.getVoteCount();
                    }
                    notifyDataSetChanged();
                }
            }
        }
    }
    public void onEventMainThread(GuideTabCountEvent event) {
        if(mData != null || event != null) {
            for(HomeRecommendItem item : mData) {
                PbBean.PbGuideSimple recommend = item.recommend;
                if (recommend.getId() == event.getGuideId()) {
                    if(event.isCommentType()){
                        item.commentCount = event.getCommentCount();
                    }else if(event.isLikeType()){
                        item.likeCount = event.getLikeCount();
                    }
                    notifyDataSetChanged();
                }
            }
        }
    }

    private class TextHolder extends AbsViewHolder<HomeRecommendItem> {
        private ItemTextRecommendBinding mBinding;

        public TextHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            mBinding.topLayout.tvName.setOnClickListener(this);
            mBinding.topLayout.ivHeader.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final HomeRecommendItem item) {
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, mBinding.ivRecommendNew, item);
            mBinding.tvTime.setVisibility(View.VISIBLE);
            mBinding.tvTime.setText(DateUtils.getFriendlyTime(item.recommend.getTime()));
        }

        @Override
        public void onClick(View v) {
            UserInfoActivity.start(v.getContext(), item.recommend.getUserInfo().getUserId(), item.recommend.getUserInfo().getIsDesigner());
            return;
        }
    }
    private class SingleImageHolder extends AbsViewHolder<HomeRecommendItem> {
        private ItemMultiGridImageBinding mBinding;

        public SingleImageHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            mBinding.topLayout.tvName.setOnClickListener(this);
            mBinding.topLayout.ivHeader.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final HomeRecommendItem item) {
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, mBinding.ivRecommendNew, item);

            final List<PbBean.PbImage> images = item.recommend.getImageListList();
            final ArrayList<String> urls = new ArrayList<>();
                        for (PbBean.PbImage image : images) {
                            urls.add(image.getUrl());
                        }
            final PbBean.PbGuideSimple recommend = item.recommend;
            mBinding.gridimageLayout.setImage(urls);
            mBinding.gridimageLayout.setOnImageItemClickListener(new GridImageThreeLayout.OnImageItemClickListener() {
                @Override
                public void onImageClick(int index, ImageView imageView) {
                    GuideDetailActivity.startActivityForTab(imageView.getContext(), recommend.getId(), 0);
                }
            });

            mBinding.tvTime.setVisibility(View.VISIBLE);
            mBinding.tvTime.setText(DateUtils.getFriendlyTime(item.recommend.getTime()));
        }

        @Override
        public void onClick(View v) {
            UserInfoActivity.start(v.getContext(), item.recommend.getUserInfo().getUserId(), item.recommend.getUserInfo().getIsDesigner());
            return;
        }
    }
    private class MultiImageHolder extends AbsViewHolder<HomeRecommendItem> {
        private ItemMultiGridImageBinding mBinding;

        public MultiImageHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            mBinding.topLayout.tvName.setOnClickListener(this);
            mBinding.topLayout.ivHeader.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final HomeRecommendItem item) {
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, mBinding.ivRecommendNew, item);
            final List<PbBean.PbImage> images=item.recommend.getImageListList();
            final ArrayList<String> urls = new ArrayList<String>();
                        for (PbBean.PbImage image : images) {
                            urls.add(image.getUrl());
                        }
            final PbBean.PbGuideSimple recommend = item.recommend;
            mBinding.gridimageLayout.setImage(urls);
            mBinding.gridimageLayout.setOnImageItemClickListener(new GridImageThreeLayout.OnImageItemClickListener() {
                @Override
                public void onImageClick(int index, ImageView imageView) {
                    GuideDetailActivity.startActivityForTab(imageView.getContext(), recommend.getId(), 0);
                }
            });
            mBinding.tvTime.setVisibility(View.VISIBLE);
            mBinding.tvTime.setText(DateUtils.getFriendlyTime(item.recommend.getTime()));
        }

        @Override
        public void onClick(View v) {
            UserInfoActivity.start(v.getContext(), item.recommend.getUserInfo().getUserId(), item.recommend.getUserInfo().getIsDesigner());
            return;
        }
    }
}

